﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using VacationDaysDatabase;
using VacationDaysCommons;
using System.ComponentModel;

namespace VacationDaysClient.CustomControls
{
    /// <summary>
    /// Interaction logic for PMSelector.xaml
    /// </summary>
    public partial class PMSelector : UserControl
    {
        public ObservableCollection<VDEmployee> _projectManagersCollection = new ObservableCollection<VDEmployee>();
        public ObservableCollection<VDEmployee> ProjectManagersCollection { get { return _projectManagersCollection; } }

        public PMSelector()
        {
            if (!DesignerProperties.GetIsInDesignMode(new DependencyObject()))
            {
                using (VDUserServiceClient vu = new VDUserServiceClient())
                {
                    var projectManagers = vu.GetProjectManagers();
                    _projectManagersCollection.Clear();

                    foreach (var projectMan in projectManagers)
                    {
                        _projectManagersCollection.Add(projectMan);
                    }

                }
            }
            InitializeComponent();
        }

        public VDEmployee SelectedPM
        {
            get { return (comboBox1.SelectedItem as VDEmployee ?? new VDEmployee());}
            set {
                  var foundPM = _projectManagersCollection.SingleOrDefault(pm => pm.Id == value.Id);
                  if (foundPM != null)
                      comboBox1.SelectedItem = foundPM;
                }
        }
    }
}
